<?php
/**
 * KhoHang 
 * - Define class KhoHang
 * @author ANHTAM
 * @since 1.1 - Sep 27, 2011
 * $Id: $
 */
include_once(DOCUMENT_ROOT . "/lib/core/BaseProcess.class.php");
class KhoHang extends BaseProcess {
	public function __construct() {
		parent::__construct("khohang");
		$this->primaryKeyName = "id";
	}
	
	public function checkMaHang($code) {
		$sql = "SELECT count(*) as count_id FROM " . $this->tableName . " WHERE maSanPham = '" . mysql_real_escape_string($code) . "'";
		$rs = $this->execute($sql, ADODB_FETCH_ASSOC);
		return ($rs && $rs->fields['count_id'] > 0) ? false : true;
	}
	
	public function totalSanPham($idSP, $flag = false) {
		if ($flag) {
			$sql = "SELECT soluong as count_id FROM sanpham WHERE id = '" . mysql_real_escape_string($idSP) . "'";
		} else {
			$sql = "SELECT count(maSanPham) as count_id FROM " . $this->tableName . " WHERE sanpham_id = '" . mysql_real_escape_string($idSP) . "' AND del='0'";
		}
		$rs = $this->execute($sql, ADODB_FETCH_ASSOC);
		return ($rs) ? $rs->fields['count_id'] : 0;
	}
	
	public function getInformationCard($atrr, $field) {
		$sql = "SELECT " . $field . " FROM sp_category as d, sp_sub_category as c, sanpham as s, giohang as gh, khohang as kh";
		$sql .= " WHERE d.id=s.iddm AND c.id=s.iddmc AND s.id=gh.gh_sanpham AND s.id=kh.sanpham_id";
		foreach ($atrr as $key=>$value) {
        	if (is_array($value)) {
        		$value['value'] = "'" . mysql_real_escape_string($value['value']) . "'";
	            $sql .= " AND " . $key . $value['math'] . $value['value'];	
        	} else {
        		if ($value !== null) {
	                $value = "'" . mysql_real_escape_string($value) . "'";
	                $sql .= " AND $key=$value";
	            }else {
	                $sql .= " AND $key IS NULL";
	            }	
        	}
        }

        $rs = $this->execute($sql, ADODB_FETCH_ASSOC);
        $result = array();
        if($rs) {
            while (! $rs->EOF && $item = $rs->FetchRow()) { 
                $result = $item;
            }
        }
        return $result;
	}
}
?>